package com.avicit.zipkinkafkaesserver.utils;


import com.avicit.zipkinkafkaesserver.enums.ESIndexsEnum;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理
 */
public class TimeUtils {

    public  enum DatePattern {
        ALLTIME("yyyy-MM-dd HH:mm:ss"),
        TIME("HH:mm:ss")
        ;
        private  String pattern;
        DatePattern(String s) {
            this.pattern = s;
        }
    }

    /**
     *  字符串日期--> 毫秒
     * @param dateTime 字符串日期
     * @param pattern 日期格式
     * @return
     */
    public static long dateTimeToMilliSecond (String dateTime, DatePattern pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern.pattern);
        LocalDateTime parse = LocalDateTime.parse(dateTime, dateTimeFormatter);
        //时区
        return parse.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    //获取当前日期yyyy-MM-dd
    public static String nowDateString() {
        return LocalDate.now()+"";
    }

    /**
     *当前日期 往前退几天
     * @param i
     * @return
     */
    public static String backFor(int i) {
        return LocalDate.now().plusDays(-i) + "";
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String nowTime() {
        return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));

    }

    /**
     * 时间戳-->日期
     * @param timestamp
     * @return
     */
    public static String timestamToDatetime(long timestamp){
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern(DatePattern.ALLTIME.pattern));
    }

    /**
     * 获取小时
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }
    /**
     * 功能描述：返回日
     *
     * @param date
     *            Date 日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }


    public static Date getDate(long timestamp) throws ParseException {
        //时间戳转化为Sting或Date
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = format.format(timestamp);
        Date date=format.parse(d);
        return date;
    }


    /**
     * 功能描述：返回月
     *
     * @param date
     *            Date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }


    /**
     * 功能描述：返回分
     *
     * @param date
     *            日期
     * @return 返回分钟
     */
    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 返回秒钟
     *
     * @param date
     *            Date 日期
     * @return 返回秒钟
     */
    public static int getSecond(Date date) {
        Calendar  calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    public static long getNextDayZero(long timstap) throws ParseException {
        Date date = getDate(timstap);
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = yyyyMMdd.format(date);
        Date result = simpleDateFormat.parse(dateStr +" 00:00:00");
        return result.getTime();

    }

    public static String getYyyyMMdd(long timstap) throws ParseException {
        Date date = getDate(timstap);
        SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = yyyyMMdd.format(date);
        return dateStr;

    }






    public static void main(String[] args) throws ParseException {
//        String s = nowDateString();
//        LocalDate localDate = LocalDate.now().plusDays(-1);
//        s = s+ " 00:00:00";
//        long time = dateTimeToMilliSecond(s, DatePattern.ALLTIME);
//        System.out.println(s);
//        String string = new StringBuffer(TimeUtils.nowDateString()).append(" ").append("00:00:00").toString();
//        System.out.println(dateTimeToMilliSecond(string,DatePattern.ALLTIME));
//        System.out.println(localDate);
//
//        String indexPrefix = ESIndexsEnum.ZipkinInterfaceCountPrefix.getIndexPrefix();
//        LocalDate yes = LocalDate.now().plusDays(-1);
//        System.out.println(indexPrefix+"_"+yes);
//        LocalDateTime localDateTime = LocalDateTime.parse("2019-06-13 09:19:00", DateTimeFormatter.ofPattern(DatePattern.ALLTIME.pattern));
//        String time =localDateTime.getYear()+localDateTime.getMonthValue()+localDateTime.getDayOfMonth() + "";
//        System.out.println(time);


//        Date date = getDate(1560282360000L);
//        System.out.println(date);
//                System.out.println(getHour(date));
        System.out.println(getYyyyMMdd(1560282360000L));
        System.out.println(nowDateString());





    }
}



